<?php
namespace Backend\Controller;
use Think\Controller;

/**
 * 代码位分组管理控制器
 */
class CodeGroupController extends BaseController
{
    public function index(){
        $model =D('CodeGroup');
        $data=$model ->get_code_group_list();
        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));
        //var_dump($data['page']);exit;
		$this->display('index');
    }

    public  function  create(){
        $data=$_POST;

        if($data) {
            $model = D('CodeGroup');
            if(!$data = $model->create()){
                $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError()]);
                exit;
            }else{

                if($id= $model->add())
                {
                    $this->ajaxReturn(['status' => 'success', 'msg' => '添加成功！']);
                    exit;
                }else{
                    $this->ajaxReturn(['status' => 'failed', 'msg' =>$model->getError() ]);
                    exit;
                }
            }

        }
        $result =D('Media')->field('id,name')->order('id desc')->select();
        $this->assign('result',$result);

        $this->display();
    }



    public  function  edit(){
        $id = I('get.id');
        $data=$_POST;
       // var_dump($id);exit;
        if($data) {
            $model = D('CodeGroup');
            if (!$model->create()) {
                $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError() ]);
            }else{

                if($model->save() !== FALSE) {
                    $this->ajaxReturn(['status' => 'success', 'msg' => '修改成功！']);
                    exit;

                }else{
                    $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError() ]);
                    exit;
                }
            }
        }

            $where['id'] = ['eq', $id];
            $group = D('CodeGroup')->where($where)->find();
            //var_dump($group);exit;
            $this->assign('info', $group);

            $result =D('Media')->field('id,name')->order('id desc')->select();
            $this->assign('result',$result);


        $this->display();
    }

    public  function  delete() {

        if(!$_POST['id']){
            $this->ajaxReturn(['status' => 'failed', 'msg' => '操作有误，重新选择！']);
            die;

        }


        $where ='id in('.trim($_POST['id'],',').')';
        $model = D('CodeGroup');
        $res =$model->where($where)->delete();
        if ($res) {
            $this->ajaxReturn(['status' => 'success', 'msg' => '操作成功！']);
        } else {
            $this->ajaxReturn(['status' => 'failed', 'msg' => '操作失败！']);
        }
    }
}